﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using System.Security;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Threading;
using SiteChecker.Interfaces;
using SiteChecker.Helpers;

namespace SiteChecker.Helpers
{
    public class RobotsTxtParser
    {
        protected ISiteStatus site;
        public RobotsTxtParser(ISiteStatus Site)
        {
            this.site = Site;
        }
        public void Parse()
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://" + this.site.URL + "/robots.txt", UriKind.Absolute));
            request.BeginGetResponse(new AsyncCallback(ReadCallback), request); 
        }
        private void ReadCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
                
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string resultString = reader.ReadToEnd();
                    if (!string.IsNullOrWhiteSpace(resultString))
                    {
                        UIThread.Run(delegate
                        {
                            if (response.ContentType == "text/plain")
                            {
                                this.site.RobotsStatus = true;
                                this.site.AllAgents = this.CheckString("User-agent:( )*\\*", resultString);
                                this.site.AllPaths = this.CheckString("(Allow:( )*/)|(Disallow:( )*$)", resultString);
                            }
                        });
                    }
                }
            }
            catch (WebException webException)
            {
                HttpWebResponse response = (HttpWebResponse)webException.Response;
                if (response.StatusCode == HttpStatusCode.NotFound)
                    UIThread.Run(delegate
                        {
                            this.site.RobotsStatus = false;
                        });
                else
                    throw;
            }
            catch (TargetInvocationException ex)
            {
                if (ex.InnerException is WebException)
                {
                    this.site.RobotsStatus = false;
                }
                else if (ex.InnerException is SecurityException)
                {
                    //TODO: Raise an error to indicate the app needs to be run out of browser for security reasons
                }
            }
        }
        private bool CheckString(string regEx, string robotsFileString)
        {
            Regex regex = new Regex(regEx, RegexOptions.IgnoreCase);
            return regex.IsMatch(robotsFileString);
        }
    }
}
